﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FootstepGenerator : MonoBehaviour
{
    private float m_DistanceInterval =0f;
    private Vector3 m_LastPosition;
    public SpriteRenderer m_FootstepTemplate;
    public float m_LifeTime = 5f;
    void Start()
    {
        
    }

    /// <summary>
    /// 测试用，之后还是用同一Update框架吧
    /// </summary>
    void Update()
    {
        if (SpriteDrawingManager.Inst.IsInit == false) return;
        var pos = m_FootstepTemplate.transform.position;
        var dist = Vector3.Distance(pos, m_LastPosition);

        if (dist > 3f)
        {
            m_LastPosition = pos;
            dist = 0;
        }

        if (dist > m_DistanceInterval)
        {
            
            //var drifting = GetDriftingFactor();//漂移境界
            //if (drifting > 0.0f && Mathf.Abs(pos.y) < m_GroundMaxHeight)
            {
                var inst = SpriteDrawingManager.Inst.AcquireInst(m_FootstepTemplate, m_LifeTime);
                m_FootstepTemplate.gameObject.SetActive(false);
                var scale = m_FootstepTemplate.transform.localScale;
                var color = m_FootstepTemplate.color;
    
                scale.y *= dist / m_DistanceInterval;
                //color.a = Mathf.Clamp01(drifting * 5.0f);
                color.a = 1;
                inst.position = m_FootstepTemplate.transform.position;
                //inst.rotation = Quaternion.LookRotation((m_LastPosition - inst.position).FlattenY()) * m_FootstepTemplate.transform.localRotation;
                inst.scale = scale;
                inst.color = color;
//                if (!m_LastDrifting)
//                {
//                    if (OnDriftingChanged != null)
//                    {
//                        OnDriftingChanged(true);
//                    }
//                    m_LastDrifting = true;
//                }
            }
//            else
//            {
//                if (m_LastDrifting)
//                {
//                    if (OnDriftingChanged != null)
//                    {
//                        OnDriftingChanged(false);
//                    }
//                    m_LastDrifting = false;
//                }
//            }
            m_LastPosition = pos;
        }
    }
}
